{template 'common/header'}
<script>
    $(function(){
        $(".add").click(function(){
            var name = $("#name").val();
            var top_id = $("#top_id").val();
            if(name != ''){
                var url = "{php echo $this->createWebUrl('system',array('op' => 'add_department'))}";
                $.ajax({
                    type : "post",
                    url : url,
                    data : {'name' : name, 'top_id' : top_id},
                    success : function(data){
                        if(data == 111){
                            location.reload();
                        }else{
                            alert(data);
                        }
                    }
                });
            }else{
                alert("请输入部门名！");
            }
        });
        $(".department-name").blur(function(){
            var name = $(this).val();
            var department_id = $(this).parent().parent().attr("department_id");
            var url = "{php echo $this->createWebUrl('system',array('op' => 'change_department'))}";
            $.ajax({
                type : "post",
                url : url,
                data : {'name' : name, 'department_id' : department_id},
                success : function(data){

                }
            });
        });
        $(".top-department").change(function(){
            var top_id = $(this).val();
            var department_id = $(this).parent().parent().attr("department_id");
            var url = "{php echo $this->createWebUrl('system',array('op' => 'change_department'))}";
            $.ajax({
                type : "post",
                url : url,
                data : {'top_id' : top_id, 'department_id' : department_id},
                success : function(data){

                }
            });
        });
        $(".del-department").click(function(){
            if(confirm("是否确定删除？")){
                var department_id = $(this).parent().parent().attr("department_id");
                var url = "{php echo $this->createWebUrl('system',array('op' => 'change_department'))}";
                $.ajax({
                    type : "post",
                    url : url,
                    data : {'type' : 'del', 'department_id' : department_id},
                    success : function(data){
                        location.reload();
                    }
                });
            }
        });
    });
</script>
<ul class="nav nav-tabs">
    <li ><a href="{php echo $this->createWebUrl('system', array('op'=>'index'))}">账号管理</a></li>
    <li ><a href="{php echo $this->createWebUrl('system', array('op'=>'add'))}">新建账号</a></li>
    <li class="active"><a href="{php echo $this->createWebUrl('system', array('op'=>'department'))}">部门管理</a></li>
</ul>
<div>
    <div>
        <div class="col-xs-3">
            <input type="text" placeholder="请输入部门名称" class="form-control" id="name" />
        </div>
        <div class="col-xs-3">
            <select class="form-control" id="top_id">
                <option value="0">根部门</option>
                {loop $deps $dep}
                <option value="{$dep['id']}">{$dep['name']}</option>
                {/loop}
            </select>
        </div>
        <div>
            <button class="btn btn-primary add">新增</button>
        </div>
    </div>
    <br />
    <div>
        <table class="table">
            <tr>
                <th>部门</th>
                <th>上级</th>
                <th>操作</th>
            </tr>
            {loop $departments $department}
            <tr department_id="{$department['id']}">
                <td>
                    <input type="text" value="{$department['name']}" class="form-control department-name" />
                </td>
                <td>
                    <select class="form-control top-department" >
                        <option value="0">根部门</option>
                        {loop $deps $dep}
                        {if $dep['id'] != $department['id']}
                        <option value="{$dep['id']}" {if $dep['id'] == $department['top_id']} selected="selected" {/if}>{$dep['name']}</option>
                        {/if}
                        {/loop}
                    </select>
                </td>
                <td>
                    <button class="btn btn-danger del-department">删除</button>
                </td>
            </tr>
            {/loop}
        </table>
    </div>
</div>



{template 'common/footer'}